package cn.lil.servlet;

import cn.lil.model.Order;
import cn.lil.model.User;
import cn.lil.service.OrderService;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.SQLException;
import java.time.ZoneId;
import java.util.Date;
@WebServlet(name="OrderConfrimServlet",value = "/OrderConfrimServlet")
public class OrderConfrimServlet extends HttpServlet {
    OrderService orderService = new OrderService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order o = (Order) request.getSession().getAttribute("orderlist");
        o = (Order) request.getParameterMap();
        o.setDatetime(new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        o.setStatus(1);
        o.setUserId(((User) request.getSession().getAttribute("user")).getId());
        try {
            orderService.addOrder(o);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getSession().removeAttribute("orderlist");
        request.setAttribute("msg","订单支付成功！");
        request.getRequestDispatcher("/order_success.jsp").forward(request,response);
    }
}
